import {
  Provide,
  Body,
  ALL,
  Inject,
  Post,
  Get,
  Query,
  Validate,
} from '@midwayjs/decorator';
import { Context } from 'egg';
import { CoolController, BaseController } from 'midwayjs-cool-core';
import { LoginDTO } from '../../dto/login';
import { LoginVO } from '../../vo/login';

import { Result } from '../../vo/result';
import { BaseSysLoginService } from '../../service/sys/login';
import { BaseSysParamService } from '../../service/sys/param';
import { CreateApiDoc } from '@midwayjs/swagger';
export class MidwayController extends BaseController {
  /**
     * 成功返回
     * @param data 返回数据
     */
  success<T>(data?: T) {
    let result = new Result<T>()
    let ok = this.ok()
    result.code = ok.code;
    result.message = ok.message;
    result.data = data;
    return result;
  }
}
/**
 * 不需要登录的后台接口 
 */
@Provide()
@CoolController('/admin/base/open', {
  tagName: 'admin-base-open',
  description: '不需要登录的后台接口'
})
export class BaseOpenController extends MidwayController {
  @Inject()
  baseSysLoginService: BaseSysLoginService;

  @Inject()
  baseSysParamService: BaseSysParamService;

  @Inject()
  ctx: Context;

  /**
   * 根据配置参数key获得网页内容(富文本)
   */
  @Get('/html')
  async htmlByKey(@Query() key: string) {
    this.ctx.body = await this.baseSysParamService.htmlByKey(key);
  }

  /**
   * 登录
   * @param login
   */
  @Post('/login')
  @Validate()
  @(CreateApiDoc()
    .summary('登录-gqf')
    .description('登录-描述')
    .respond(200, '正常返回', 'json', {
      example: require('../../json/login.json')
    })
    .build())
  async login(@Body(ALL) login: LoginDTO): Promise<Result<LoginVO>> {
    return this.success(await this.baseSysLoginService.login(login));
  }

  /**
   * 获得验证码
   */
  @Get('/captcha')
  @(CreateApiDoc()
    .summary('获得验证码-gqf')
    .description('获得验证码-描述')
    .param('类型', {
      name: 'type',
      type: 'string',
      required: true,
      example: 'base64',
      description: '返回类型base64'
    })
    .param('宽度', {
      name: 'width', type: 'number', required: true,
      example: 150
    })
    .param('高低', {
      name: 'height', type: 'number', required: true,
      example: 50
    })
    .respond(200, '正常返回', 'json', {
      properties:{
        "code":{
          "type": "number",
          "description":'状态码',
          "enum": [
            1000,
            2000,
            3000
          ]
        }
      },
      example: {
        "code": 1000,
        "message": "success",
        "data": {
          "captchaId": '',
          "data": ''
        }
      }
    })
    .build())
  async captcha(
    @Query() type: string,
    @Query() width: number,
    @Query() height: number
  ): Promise<Result<any>> {
    return this.success(await this.baseSysLoginService.captcha(type, width, height));
  }

  /**
   * 刷新token
   */
  @Get('/refreshToken')
  async refreshToken(@Query() refreshToken: string) {
    return this.ok(await this.baseSysLoginService.refreshToken(refreshToken));
  }
}
